home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / communication / bbs / termv4.6 / extras / source / term-source.lha / Speech.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-18  |  2.9 KB  |  171 lines

  1. /*
  2. **    Speech.c
  3. **
  4. **    Speech support routines
  5. **
  6. **    Copyright © 1990-1996 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. */
  9.  
  10. #ifndef _GLOBAL_H
  11. #include "Global.h"
  12. #endif
  13.  
  14.     /* Local symbols. */
  15.  
  16. STATIC struct MsgPort        *NarratorPort;
  17. STATIC struct narrator_rb    *NarratorRequest;
  18. STATIC UBYTE             SpeechString[512],
  19.                  TranslatedString[512];
  20. STATIC BOOLEAN             DidSpeak;
  21.  
  22.     /* DeleteSpeech():
  23.      *
  24.      *    Free the data associated with the speech function.
  25.      */
  26.  
  27. VOID
  28. DeleteSpeech()
  29. {
  30.     if(DidSpeak)
  31.     {
  32.         if(!CheckIO(NarratorRequest))
  33.             WaitIO(NarratorRequest);
  34.     }
  35.  
  36.     if(NarratorRequest)
  37.     {
  38.         if(NarratorRequest -> message . io_Device)
  39.             CloseDevice(NarratorRequest);
  40.  
  41.         DeleteIORequest(NarratorRequest);
  42.  
  43.         NarratorRequest = NULL;
  44.     }
  45.  
  46.     if(NarratorPort)
  47.     {
  48.         DeleteMsgPort(NarratorPort);
  49.  
  50.         NarratorPort = NULL;
  51.     }
  52.  
  53.     if(TranslatorBase)
  54.     {
  55.         CloseLibrary(TranslatorBase);
  56.  
  57.         TranslatorBase = NULL;
  58.     }
  59.  
  60.     DidSpeak = FALSE;
  61. }
  62.  
  63.     /* CreateSpeech():
  64.      *
  65.      *    Open translator.library and narrator.device, perform
  66.      *    the necessary setups for the speech function.
  67.      */
  68.  
  69. BOOL
  70. CreateSpeech()
  71. {
  72.     if(TranslatorBase = OpenLibrary("translator.library",0))
  73.     {
  74.         if(NarratorPort = CreateMsgPort())
  75.         {
  76.             if(NarratorRequest = (struct narrator_rb *)CreateIORequest(NarratorPort,sizeof(struct narrator_rb)))
  77.             {
  78.                 STATIC UBYTE AnyChannel[] =
  79.                 {
  80.                     LEFT0F,
  81.                     LEFT1F,
  82.                     RIGHT0F,
  83.                     RIGHT1F
  84.                 };
  85.  
  86.                     /* Any channel will do. */
  87.  
  88.                 NarratorRequest -> ch_masks        = AnyChannel;
  89.                 NarratorRequest -> nm_masks        = sizeof(AnyChannel);
  90.  
  91.                     /* This is a write request. */
  92.  
  93.                 NarratorRequest -> message . io_Command    = CMD_WRITE;
  94.                 NarratorRequest -> message . io_Data    = (APTR)SpeechString;
  95.  
  96.                 if(!OpenDevice("narrator.device",0,NarratorRequest,0))
  97.                 {
  98.                     SpeechSetup();
  99.  
  100.                     return(TRUE);
  101.                 }
  102.             }
  103.         }
  104.     }
  105.  
  106.     DeleteSpeech();
  107.  
  108.     return(FALSE);
  109. }
  110.  
  111.     /* Say(STRPTR String,...):
  112.      *
  113.      *    Translate a string into phonemes and speak it.
  114.      */
  115.  
  116. VOID __stdargs
  117. Say(STRPTR String,...)
  118. {
  119.     if(SpeechConfig . Enabled && English)
  120.     {
  121.         if(!TranslatorBase)
  122.             CreateSpeech();
  123.  
  124.         if(TranslatorBase)
  125.         {
  126.             va_list VarArgs;
  127.  
  128.             if(DidSpeak)
  129.             {
  130.                 if(!CheckIO(NarratorRequest))
  131.                     WaitIO(NarratorRequest);
  132.             }
  133.  
  134.             va_start(VarArgs,String);
  135.             VSPrintf(TranslatedString,String,VarArgs);
  136.             va_end(VarArgs);
  137.  
  138.             if(!Translate(TranslatedString,strlen(TranslatedString),SpeechString,511))
  139.             {
  140.                 NarratorRequest -> message . io_Length = strlen(SpeechString);
  141.  
  142.                 ClrSignal(PORTMASK(NarratorRequest -> message . io_Message . mn_ReplyPort));
  143.  
  144.                 SendIO(NarratorRequest);
  145.  
  146.                 DidSpeak = TRUE;
  147.             }
  148.             else
  149.                 DidSpeak = FALSE;
  150.         }
  151.     }
  152. }
  153.  
  154.     /* SpeechSetup():
  155.      *
  156.      *    Transfer the configuration data into the setup.
  157.      */
  158.  
  159. VOID
  160. SpeechSetup()
  161. {
  162.     if(NarratorRequest)
  163.     {
  164.         NarratorRequest -> rate        = SpeechConfig . Rate;
  165.         NarratorRequest -> pitch    = SpeechConfig . Pitch;
  166.         NarratorRequest -> sex        = SpeechConfig . Sex;
  167.         NarratorRequest -> volume    = SpeechConfig . Volume;
  168.         NarratorRequest -> sampfreq    = SpeechConfig . Frequency;
  169.     }
  170. }
  171.